var ns, bmLocation;
app = new Vue({
    el: '#app',
    data: {
        list: [],
        form: {
            pageindex: 0,
            pagesize: 10,
            // status: 1, // 1-任务列表 2-新增记录
            PlanType: 1,
            TaskStatus:0//0-未开始,1-进行中,2-已结束
        },
        total: {
            ProcessCount: 0,
            UnStart:0,
            Doing:0,
        },
        current_item_length: 0,
        is_searching: false,
        scroll_top: 0,
        can_scroll: false,
        showTop:false,
        showsearch:false,

    },
    methods: {
        get_data: function() {
            var that = this;
            that.is_searching= true,
            that.can_scroll = false;
            var options = {};
            options.action = "getequipmentlist";
            options.pagesize = that.form.pagesize;
            options.pageindex = that.form.pageindex;
            options.PlanType = that.form.PlanType;
            options.TaskStatus =that.form.TaskStatus;
            ns.post(options, function(succeed, data, err) {
                that.is_searching= false;
                if (succeed) {
                    if (data.list.length == that.form.pagesize) {
                        that.can_scroll = true;
                    }
                    if (that.form.pageindex == 0) {
                        that.list = data.list;

                    } else {
                        that.list = that.list.concat(data.list);
                    }
                    app.current_item_length = that.list.length
                    that.total.UnStart = data.UnStart;
                    that.total.Doing = data.Doing;
                } else if (err) {
                    // that.list = [];
                    that.current_item_length = 0;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        select_status: function(status) {
            var that = this;
            that.showTop=false;
            that.form.pageindex=0;
            that.form.TaskStatus = status;
            that.list = [];
            that.get_data();
        },
        open_list: function(item) {
            var that = this;
            var url = 'equipment_list_frm';
            var title = item.DevicePlanName;
            ns.openWin(url, title, {
                id: item.ID
            });
        },
        open_add: function () {
            var that = this;
            ns.openWin('equipment_addfrm', '新增记录');
        },
        open_select:function(){
            var that=this;
            that.showTop=!that.showTop;
        },
        doHideSearch:function(){
            var that=this;
            that.showTop=false;
            that.showsearch=false;
        },
        select_allstatus:function(){
            var that=this;
            that.showsearch=!that.showsearch;
        },
        open_history:function(){
            var that = this;
            ns.openWin('equipment_list_Record', '设备作业');
        },
        doComplete(item){
            var that = this;
            if(item.completeCount!=item.count){
                api.toast({
                    msg: '存在未完成的工作点，请完成所有工作点后再进行提交。',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            api.confirm({
                title: '提示',
                msg: '完成任务后不能再修改工作记录，如需修改需要使用重新执行功能。确认要完成工作任务吗？',
                buttons: ['确定', '取消']
            }, function (ret, err) {
                if (ret) {
                    if (ret.buttonIndex == 1) {
                        //Type-类型：1-设备 3-安防巡更 4-环境保洁
                        var options = { action: 'taskcomplete', ID: item.ID, Type: 1 };
                        ns.post(options, function (succeed, data, err) {
                            if (succeed) {
                                if (data.status) {
                                    that.get_data();
                                }
                            } else {
                                api.toast({
                                    msg: err,
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            }
                        })
                    }
                }
            });
        },
    },
    
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.get_data();
    api.addEventListener({
        name: 'devicetaskcomplete'
    }, function(ret, err) {
        app.get_data();
    });
    api.addEventListener({
        name: 'startplantaskdone'
    }, function(ret, err) {
        app.get_data();
    });
    api.addEventListener({
        name: 'do_open_add'
    }, function (ret, err) {
        app.open_add();
    });
    api.addEventListener({
        name: 'do_open_select'
    }, function (ret, err) {
        app.open_select();
    });
    api.addEventListener({
        name: 'do_open_fee_history'
    }, function (ret, err) {
        app.open_history();
    });
    ns.init_pull();
};

